# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

cmake_minimum_required(VERSION 3.5)

# Project
project(OrtMT5 C CXX)
string(APPEND CMAKE_CXX_FLAGS " /W4")

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(STATUS "64 bit")
    set(is_x64 TRUE)
endif()

if (CMAKE_SIZEOF_VOID_P EQUAL 4)
    message(STATUS "32 bit")
    set(is_x64 FALSE)
endif()


if(NOT ONNXRUNTIME_ROOTDIR)
    if (is_x64)
        #set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntime-win-x64-1.16.3")
        #set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntimegit/onnxruntime")
        if (NOT PROVIDER)
            #set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntime-win-x64-1.16.3")
            set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntime_my_win_x64")
        endif()
        if(PROVIDER STREQUAL "dml")
            set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntime-dml-1.16.1-x64")
            add_compile_options(-DUSE_DML)
        endif()
    endif()
    if (NOT is_x64)
        #set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntime-win-x86-1.16.2")
        #set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntimegit/onnxruntime")
        set(ONNXRUNTIME_ROOTDIR "D:/libs/onnxruntime_my_win_x86")
    endif()
endif()
if(NOT SENTENCEPIECE_ROOTDIR)
    if (is_x64)
        set(SENTENCEPIECE_ROOTDIR "D:/libs/sentencepiece-0.1.99-win64")
    endif()
    if (NOT is_x64)
        set(SENTENCEPIECE_ROOTDIR "D:/libs/sentencepiece-0.1.99-win32")
    endif()
endif()
if(NOT ARGPARSE_ROOTDIR)
    set(ARGPARSE_ROOTDIR "D:/libs/argparse")
endif()
if(NOT ICU_ROOTDIR)
    if (is_x64)
        set(ICU_ROOTDIR "D:/libs/icu4c-74_1-Win64-MSVC2022")
    endif()
    if (NOT is_x64)
        set(ICU_ROOTDIR "D:/libs/icu4c-71_1-Win32-MSVC2019")
    endif()
endif()
include_directories("${ONNXRUNTIME_ROOTDIR}/include" "${ONNXRUNTIME_ROOTDIR}/include/onnxruntime/core/session")
include_directories("${SENTENCEPIECE_ROOTDIR}/include")
include_directories("${ARGPARSE_ROOTDIR}/include")
include_directories("${ICU_ROOTDIR}/include")
link_directories("${ONNXRUNTIME_ROOTDIR}/lib")
link_directories("${SENTENCEPIECE_ROOTDIR}/lib")
if (is_x64)
    link_directories("${ICU_ROOTDIR}/lib64")
endif()
if (NOT is_x64)
    link_directories("${ICU_ROOTDIR}/lib")
endif()
find_package(Threads) 

if (CMAKE_BUILD_TYPE STREQUAL Debug)
    ADD_DEFINITIONS(-DDEBUG)
endif()
if (CMAKE_BUILD_TYPE STREQUAL Release)
    ADD_DEFINITIONS(-DRELEASE)
endif()

add_executable(ortmt5 "OrtMT5.cpp" "OrtMT5.h" "utils.cpp" "utils.h")
add_library(ortmtlib SHARED "OrtMTLib.c" "OrtMTLib.h")
add_library(splib SHARED "SpLib.cpp" "SpLib.h")

if(MSVC)
    if(is_x64)
        add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/MACHINE:X64")
    endif()
    if(NOT is_x64)
        add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/MACHINE:X86")
    endif()
endif()


target_link_libraries(ortmt5 PRIVATE onnxruntime sentencepiece icuuc icuio Threads::Threads)
target_link_libraries(ortmtlib PRIVATE onnxruntime Threads::Threads)
target_link_libraries(splib PRIVATE sentencepiece Threads::Threads)
target_compile_features(ortmt5 PRIVATE cxx_std_17)
target_compile_features(ortmtlib PRIVATE c_std_11)
target_compile_features(splib PRIVATE cxx_std_17)

#In onnxruntime deafault install path, the required dlls are in lib and bin folders
if (is_x64)
    set(DLL_DIRS "${ONNXRUNTIME_ROOTDIR}/lib;${ONNXRUNTIME_ROOTDIR}/bin;${SENTENCEPIECE_ROOTDIR}/lib;${SENTENCEPIECE_ROOTDIR}/bin;${ICU_ROOTDIR}/bin64")
endif()
if (NOT is_x64)
    set(DLL_DIRS "${ONNXRUNTIME_ROOTDIR}/lib;${ONNXRUNTIME_ROOTDIR}/bin;${SENTENCEPIECE_ROOTDIR}/lib;${SENTENCEPIECE_ROOTDIR}/bin;${ICU_ROOTDIR}/bin")
endif()
foreach(DLL_DIR IN LISTS DLL_DIRS)
  file(GLOB ALL_DLLS ${DLL_DIR}/*.dll)
  foreach(ORTDll IN LISTS ALL_DLLS)
    add_custom_command(TARGET ortmt5 POST_BUILD 
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${ORTDll}"              
                $<TARGET_FILE_DIR:ortmt5>)
    add_custom_command(TARGET ortmtlib POST_BUILD 
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${ORTDll}"              
                $<TARGET_FILE_DIR:ortmtlib>)
  endforeach()
endforeach()
